from netCDF4 import Dataset
import json
import datetime
import jaydebeapi
import jpype
#自动获取虚拟机路径
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % "mysql-connector-java-5.1.49.jar")

jar_file = 'mysql-connector-java-5.1.49.jar'
driver = 'com.mysql.jdbc.Driver'
jdbc_url1 = 'jdbc:mysql://localhost:3306/dlqx?useUnicode=true&characterEncoding=UTF-8'
# jdbc_url2 = 'jdbc:mysql://111.198.60.33:13307/dlqx_07?useUnicode=true&characterEncoding=UTF-8'
# jdbc_url3 = 'jdbc:mysql://111.198.60.33:13308/dlqx_08?useUnicode=true&characterEncoding=UTF-8'
uandp = ["root", "123456"]
#从nc中获取某一个属性的全部值，返回装有json字典对象的List
# 传入值filePath：nc文件路径  Attr 需要解析的属性的名称
def ReadFromNc(filePath):
    nc_obj = Dataset(filePath)
    #纬度
    lat = (nc_obj.variables['lat'][:])
    #经度
    lon = (nc_obj.variables['lon'][:])
    #时间（10天，维度为10）
    time = (nc_obj.variables['time'][:])
    #温度
    Temperature_height_above_ground = (nc_obj.variables['Temperature_height_above_ground'][:])
    # 需要解析的属性变量在下标为0的位置,属性代表24小时降水，温度等等
    listKeys = list(nc_obj.variables.keys())
    Attr = (nc_obj.variables[listKeys[0]])

    #获取开始的时间 因为time里面存储是[24,48,...240]  而不是具体的时间
    sinceTimeStr = nc_obj.variables['time'].units.split(" ")[-1]
    sinceTime = datetime.datetime.strptime(sinceTimeStr, '%Y-%m-%dT%H:%M:%SZ')

    ncdatadict = dict()
    ncdatadict['lat'] = lat
    ncdatadict['lon'] = lon
    ncdatadict['time'] = time
    ncdatadict['Attr'] = Attr
    ncdatadict['sinceTime'] = sinceTime
    ncdatadict['Temperature_height_above_ground'] = Temperature_height_above_ground
    return ncdatadict

def NcToJson(ncdatadict, jrsjid1, jrsjid2, jrsjid3, AttrName):
    try:
        sql4 = "UPDATE tb_jrsj SET jrsj_jxstorestatus = 1 AND jrsj_filestorestatus = 1 WHERE jrsj_id = %s " % (jrsjid1)
        sql5 = "UPDATE tb_jrsj SET jrsj_jxstorestatus = 1 AND jrsj_filestorestatus = 1 WHERE jrsj_id = %s " % (jrsjid2)
        sql6 = "UPDATE tb_jrsj SET jrsj_jxstorestatus = 1 AND jrsj_filestorestatus = 1 WHERE jrsj_id = %s " % (jrsjid3)

        conn1 = jaydebeapi.connect(driver,jdbc_url1,uandp,jar_file)
        cursor1 = conn1.cursor()
        # conn2 = jaydebeapi.connect(driver,jdbc_url2,uandp,jar_file)
        # cursor2 = conn2.cursor()
        # conn3 = jaydebeapi.connect(driver,jdbc_url3,uandp,jar_file)
        # cursor3 = conn3.cursor()

        try:
            Lat = ncdatadict['lat']
            Lon = ncdatadict['lon']
            Time = ncdatadict['time']
            Attr = ncdatadict['Attr']
            sinceTime = ncdatadict['sinceTime']
            Temperature_height_above_ground = ncdatadict['Temperature_height_above_ground']

            # print(Attr.shape)

            jsondictList = []
            TimeDimension = Attr.shape[0]
            LatDimension = Attr.shape[2]
            LonDimension = Attr.shape[3]
            for i in range(TimeDimension):
                for j in range(LatDimension):
                    for k in range(LonDimension):
                        time = (sinceTime + datetime.timedelta(hours=Time[i])).strftime("%Y-%m-%d %H:%M:%S")
                        #time = Time[i]
                        lat = Lat[j]
                        lon = Lon[k]
                        temperature = Temperature_height_above_ground[i, 0, j, k]
                        attr = Attr[i, 0, j, k]

                        NcOneObjectDict = dict()
                        NcOneObjectDict['time'] = time
                        NcOneObjectDict['lat'] = round(float(lat), 6)
                        NcOneObjectDict['lon'] = round(float(lon), 6)
                        NcOneObjectDict['temperature'] = round(float(temperature), 6)
                        NcOneObjectDict[AttrName] = 0
                        jsondictList.append(NcOneObjectDict)

                        # #=============================连接数据库==================================
                        sql1 = "INSERT INTO tb_gdfs_temperature(gdfs_temperature_time,gdfs_temperature_lat,gdfs_temperature_lon,gdfs_temperature_value,gdfs_temperature_jrsjid) \
                                VALUES ('%s', '%s', '%s', '%s', '%s')" % (time, lat, lon, temperature, jrsjid1)
                        sql2 = "INSERT INTO tb_gdfs_temperature(gdfs_temperature_time,gdfs_temperature_lat,gdfs_temperature_lon,gdfs_temperature_value,gdfs_temperature_jrsjid) \
                                VALUES ('%s', '%s', '%s', '%s', '%s')" % (time, lat, lon, temperature, jrsjid2)
                        sql3 = "INSERT INTO tb_gdfs_temperature(gdfs_temperature_time,gdfs_temperature_lat,gdfs_temperature_lon,gdfs_temperature_value,gdfs_temperature_jrsjid) \
                                VALUES ('%s', '%s', '%s', '%s', '%s')" % (time, lat, lon, temperature, jrsjid3)

                        try:
                            cursor1.execute(sql1)
                        except Exception as e:
                            print("GDFSNcToJson_OEFS_TMP(1) ----- ")

                        # try:
                        #     cursor2.execute(sql2)
                        #     # conn2.commit()
                        # except Exception as e:
                        #     print("GDFSNcToJson_OEFS_TMP(2) ----- ")

                        # try:
                        #     cursor3.execute(sql3)
                        #     # conn3.commit()
                        # except Exception as e:
                        #     print("GDFSNcToJson_OEFS_TMP(3) ----- ")
            

           # geojson 保存到数据 

            try:
                cursor1.execute(sql4)
  
            except Exception as e:
                print("GDFSNcToJson_OEFS_TMP(4) ----- ")
                
            # try:
            #     cursor2.execute(sql5)
            #     # conn2.commit()
            # except Exception as e:
            #     print("GDFSNcToJson_OEFS_TMP(5) ----- ")
                
            # try:
            #     cursor3.execute(sql6)
            #     # conn3.commit()
            # except Exception as e:
            #     print("GDFSNcToJson_OEFS_TMP(6) ----- ")

            return jsondictList

        except Exception as e:
            print("GDFSNcToJson_OEFS_TMP 文件解析异常 ----- ")

    except Exception as e:
        print("GDFSNcToJson_OEFS_TMP 数据库连接异常 ----- ")
    finally:
        cursor1.close()
        conn1.close()
        # cursor2.close()
        # conn2.close()
        # cursor3.close()
        # conn3.close()


def main(filePath,jrsjid1,jrsjid2,jrsjid3):
    ncdatadict = ReadFromNc("b.nc")
    #Js24代表过去24小时降水
    jsondictList = NcToJson(ncdatadict,jrsjid1,jrsjid2,jrsjid3,'Js24')

if __name__ == '__main__':
    main("b.nc",1,2,3)